home *** CD-ROM | disk | FTP | other *** search
/ Aminet 7 / Aminet 7 - August 1995.iso / Aminet / docs / misc / ConcNews.lha / news / general.programming / comp.lang.c_10333_000014.msg < prev    next >
Encoding:
Text File  |  1994-11-27  |  2.7 KB  |  60 lines

  1. Newsgroups: comp.lang.c
  2. Path: dd.chalmers.se!news.chalmers.se!sunic!psinntp!psinntp!chico.spr.com!psinntp!hns!jabba!hsspes1
  3. From: t_akrishnan@bart.hns.com
  4. Subject: Re: Survey: how does your compiler right-shift
  5. Message-ID: <28FEB199412014462@jabba>
  6. Sender: hsspes1@jabba (PES GROUP AT HSS)
  7. Date: Mon, 28 Feb 1994 17:01:00 GMT
  8. News-Software: VAX/VMS VNEWS 1.41    
  9. References: <2kghb7$p53@nack.craycos.com> <CLxuHM.78n@bfsec.bt.co.uk>
  10. Organization: Hughes Network Systems, Inc.
  11. Lines: 47
  12.  
  13. In article <CLxuHM.78n@bfsec.bt.co.uk>, dallison@bfsec.bt.co.uk writes...
  14. >In article p53@nack.craycos.com, ferguson@craycos.com (Scott Ferguson) writes:
  15. >>Right-shifting a signed int produces different results under different
  16. >>compilers, some fill in the upper bits with the sign bit, others just
  17. >>fill with zeros. I'm porting some code that relies heavily on the assumption
  18. >>that signed int's get filled with the sign bit (it's the mpeg encoder and
  19. >>decoder).
  20.  
  21. >Surely the whole idea of a signed shift right is to sign extend.  Is this not
  22. >defined in the ANSI standard?  
  23.  
  24. The result of E1>>E2 is E1 right shifted E2 bit positions.  In addition,
  25. if E1 is an unsigned type or a signed type with a non-negative value, 
  26. E1>>E2 is defined to yield the result E1 / (2 ^ E2) - where the ^ represents
  27. exponentiation.  The reason is presumably to ensure that 0>>E2 would always
  28. yield 0.  On a ones complement machine, an all-bit set value represents a sort
  29. of 0, in this case, the >> operator will have to do a SIGNED SHIFT to
  30. generate an all-bit-set zero as the result.
  31.  
  32.     On a twos complement machine, the right shift operator for a
  33. non-negative value is always a bit-shift, with 0 filling from the left.  Thus
  34. it may or may not be a signed shift - the effects are equivalent.
  35.  
  36.     For negative values of E1 (which implies a signed type for E1),
  37. the result is implementation-defined.  This would allow systems with no
  38. signed shift instruction to implement the >> operator efficiently.  Ones
  39. complement machines with no signed-shift are the only category which would
  40. have to go to extra lengths to implement >>.
  41.  
  42. >Most machines have instruction modifiers for
  43. >both types of shift.
  44.  
  45.     But some don't have.  The ANSI Rationale 3.3.7 (on a signed shift
  46. requirement):
  47.     " ... such a requirement might slow down fast code and ... the
  48. usefulness of sign extended shifts is marginal. (Shifting a negative
  49. twos-complement integer arithmetically right one place is NOT the same as
  50. dividing by two!)."
  51.  
  52. -----
  53. /* What is mind? No matter.
  54.    What is matter? Never mind. */
  55.  
  56. /* Ajoy Krishnan T,
  57.    Senior S/W Engineer, Hughes Software Systems,
  58.    New Delhi - 19, India.
  59.    (ajoyk%hss@lando.hns.com)*/
  60.